package ar.edu.unq.wannaplay.pages.abm.employees;

import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.dtos.SkillDTO;
import ar.edu.unq.wannaplay.employee.Experience;
import ar.edu.unq.wannaplay.pages.EmployeePage;
import ar.edu.unq.wannaplay.pages.abm.WPABM;
import ar.edu.unq.wannaplay.services.EmployeeService;
import ar.edu.unq.wannaplay.tools.SkillSelector;

public abstract class EmployeeABM extends WPABM<EmployeeDTO, EmployeePage> {
    private static final long serialVersionUID = 1L;

    @SpringBean(name = "service.employeeService")
    private EmployeeService employeeService;

    private List<SkillDTO> newSkills;

    private TextField<EmployeeDTO> idField;

    public EmployeeABM() {
    	super(EmployeePage.class);
		this.init();
	}
    
    protected EmployeeABM(final EmployeeDTO abmObject) {
        super(EmployeePage.class, abmObject);
        newSkills = abmObject.getSkills();
        this.init();
    }

    public void init() {
    	this.addButtons();
        WebMarkupContainer container = new WebMarkupContainer("data", new CompoundPropertyModel<EmployeeDTO>(
                this.getAbmObject()));
        this.makeData(container);
        this.makeSkillsTable(container);
    }

    private void makeSkillsTable(final WebMarkupContainer container) {
    	//skills que tiene el empleado
        container.add(new PageableListView<SkillDTO>("skillsTable", this.getNewSkills(), 5) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<SkillDTO> item) {
                SkillDTO skill = item.getModelObject();
                item.add(new Label("skillName", skill.getKnowledge()));
                item.add(new Label("skillExp", skill.getExperience().toString()));
            }
        });

        //para agregar skills
        final SkillSelector skillSelector = new SkillSelector();
        container.add(new TextField<String>("newSkillName", new PropertyModel<String>(skillSelector, "name")));
        container.add(new DropDownChoice<Experience>("newSkillExp", new PropertyModel<Experience>(skillSelector,
                "experience"), skillSelector.getExperiences()));

        container.add(new Link<WebPage>("addSkill") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                EmployeeABM.this.getNewSkills().add(
                        new SkillDTO(skillSelector.getExperience(), skillSelector.getName()));
                EmployeeABM.this.makeSkillsTable(container);
            }
        });
    }

    private void makeData(final WebMarkupContainer container) {
        container.add(new TextField<EmployeeDTO>("name", new PropertyModel<EmployeeDTO>(this.getAbmObject(), "name")));
        container.add(new TextField<EmployeeDTO>("surname", new PropertyModel<EmployeeDTO>(this.getAbmObject(),
                "surname")));
        this.setIdField(new TextField<EmployeeDTO>("id", new PropertyModel<EmployeeDTO>(this.getAbmObject(), "dni")));
        container.add(this.getIdField());
        container.add(new TextField<EmployeeDTO>("address", new PropertyModel<EmployeeDTO>(this.getAbmObject(),
                "address")));
        container.add(new TextField<EmployeeDTO>("telephone", new PropertyModel<EmployeeDTO>(this.getAbmObject(),
                "telephone")));
        container
                .add(new TextField<EmployeeDTO>("email", new PropertyModel<EmployeeDTO>(this.getAbmObject(), "email")));
        container.add(new TextField<EmployeeDTO>("town", new PropertyModel<EmployeeDTO>(this.getAbmObject(), "town")));
        container.add(new TextField<EmployeeDTO>("university", new PropertyModel<EmployeeDTO>(this.getAbmObject(),
                "university")));
        container.add(new TextField<EmployeeDTO>("art", new PropertyModel<EmployeeDTO>(this.getAbmObject(), "art")));
        container.add(new TextField<EmployeeDTO>("socialPlan", new PropertyModel<EmployeeDTO>(this.getAbmObject(),
                "socialPlan")));
        container.add(new TextField<EmployeeDTO>("careerPlan", new PropertyModel<EmployeeDTO>(this.getAbmObject(),
                "careerPlan")));
        container.add(new TextField<EmployeeDTO>("rank", new PropertyModel<EmployeeDTO>(this.getAbmObject(), "rank")));
        container.add(new TextField<EmployeeDTO>("modifier", new PropertyModel<EmployeeDTO>(this.getAbmObject(),
                "modifier")));
    }

    // setters&getters
    protected void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    protected EmployeeService getEmployeeService() {
        return employeeService;
    }

    public List<SkillDTO> getNewSkills() {
        return newSkills;
    }

    public void setNewSkills(final List<SkillDTO> newSkills) {
        this.newSkills = newSkills;
    }

    protected TextField<EmployeeDTO> getIdField() {
        return idField;
    }

    protected void setIdField(final TextField<EmployeeDTO> idField) {
        this.idField = idField;
    }
}
